package com.cqc.jdk8.Jutil.concurrent;

import com.cqc.eb.PersonEB;
import org.junit.Test;

import java.util.concurrent.*;

/**
 * @author cui
 * @date 2024/6/14 22:44
 * @description
 **/
public class FutureTaskTest {

    @Test
    public void f5() throws Exception {
        ExecutorService executor = Executors.newFixedThreadPool(3);
        for (int i = 0; i < 3; i++) {

        }

    }
    @Test
    public void f4() throws Exception {
        ExecutorService executor = Executors.newFixedThreadPool(3);
        PersonEB eb = new PersonEB();
        eb.setId(1);
        eb.setName("刘亦菲");

        FutureTask<PersonEB> futureTask = new FutureTask<PersonEB>(new Runnable() {
            @Override
            public void run() {
                int id = eb.getId() + 1;
                eb.setId(id);
                eb.setName(eb.getName() + id);
            }
        }, eb);
        executor.execute(futureTask);
        System.out.println("futureTask.get()=" + futureTask.get().toString());
        System.out.println("正在执行主线程任务");
        executor.shutdown();
    }

    @Test
    public void f3() throws Exception {
        ExecutorService executor = Executors.newFixedThreadPool(3);
        PersonEB eb = new PersonEB();
        eb.setId(1);
        eb.setName("刘亦菲");

        FutureTask<PersonEB> futureTask = new FutureTask<>(new Callable<PersonEB>() {
            @Override
            public PersonEB call() throws Exception {
                int id = eb.getId() + 1;
                eb.setId(id);
                eb.setName(eb.getName() + id);
                return eb;
            }
        });
        executor.execute(futureTask);
        System.out.println("futureTask.get()=" + futureTask.get().toString());
        System.out.println("正在执行主线程任务");
        executor.shutdown();
    }

    /**
     * 为了 解决get()线程 阻塞的问题，采用while循环，futureTask.isDone()才获取结果
     */
    @Test
    public void f2() throws Exception {
        ExecutorService executor = Executors.newFixedThreadPool(3);
        FutureTask<String> futureTask = new FutureTask<String>(() -> {
            System.out.println("正在执行代码线程池任务");
            try {
                TimeUnit.SECONDS.sleep(2);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }, "result");
        executor.execute(futureTask);
        System.out.println("正在执行主线程任务");
        while (true) {
            if (futureTask.isDone()) {
                System.out.println("任务结束。" + futureTask.get());
                break;
            } else {
                System.out.println("任务执行中，请等待...");
            }
        }
        executor.shutdown();
    }

    /**
     * 在调用get方法时，如果线程任务还没有执行完毕，会继续等待任务执行完毕，才返回，
     * 这样就会造成调用get方法下边的代码无法执行，造成阻塞，
     * 当然可以通过get方法的重载去设置最长等待时间
     */
    @Test
    public void f1() throws Exception {
        ExecutorService executor = Executors.newFixedThreadPool(3);
        FutureTask<String> futureTask = new FutureTask<String>(() -> {
            System.out.println("正在执行代码线程池任务");
            try {
                TimeUnit.SECONDS.sleep(2);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }, "result");
        executor.execute(futureTask);
        System.out.println(futureTask.get());
        System.out.println("正在执行主线程任务");
        executor.shutdown();
    }

}
